﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Amib.Threading;
using Dal;
using Crawling;
using Site_Extractor;
using Indexing;
using Lucene;
using Lucene.Net.Index;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;

namespace Workers
{
    /// <summary>
    /// This class finds and processes new content from a website, via RSS, or 
    /// regular crawling.
    /// </summary>
    public class DeepCrawler
    {
        Site _site;
        int _maxThreads;
        string _prefixFilePath;
        Indexer _indexer;
        DateTime _fromDate;
        LuceneManager.LuceneManager _luceneManager;
        DatabaseAbstractionLayer _dal = new DatabaseAbstractionLayer();
        IWorkItemsGroup _workGroup;

        public DeepCrawler(Site site, IWorkItemsGroup workGroup, int maxThreads, string prefixFilePath,
            DateTime fromDate, LuceneManager.LuceneManager lm)
        {
            _site = site;
            _workGroup = workGroup;
            _maxThreads = maxThreads;
            _prefixFilePath = prefixFilePath;
            _fromDate = fromDate;
            _luceneManager = lm;
            _indexer = new Indexer(_luceneManager, "", _prefixFilePath, _maxThreads, 0);
            

        }
      

        public void DeepCrawlStories(string mergeWithIndexPath)
        {
            RuleSet rs = _site[RuleSet.Story];
            if (null == rs) return;
            Crawler crawler = new Crawler(crawler_StoryDownloadedEvent, _workGroup, rs.DeepCrawlDepth, _maxThreads, rs.Encoding, CrawlUrl.CrawlUrlType.Story);
            crawler.Seed(rs.DeepCrawlUris);
            //crawler.HtmlDownloadedEvent += new EventHandler<HtmlDownloadEventArgs>(crawler_StoryDownloadedEvent);
            crawler.Crawl();

            //if (!mergeWithIndexPath.Equals(string.Empty))
            //    _luceneManager.MergeIndexes(mergeWithIndexPath);
            // _luceneManager.CloseIndexWriter();

        }

        public void DeepCrawlBlogs(string mergeWithIndexPath)
        {
            RuleSet rs = _site[RuleSet.Blog];
            if (null == rs) return;
            Crawler crawler = new Crawler(crawler_BlogDownloadedEvent, _workGroup, rs.CrawlDepth, _maxThreads, rs.Encoding, CrawlUrl.CrawlUrlType.Blog);
            crawler.Seed(rs.ContentUris);
            //crawler.HtmlDownloadedEvent += new EventHandler<HtmlDownloadEventArgs>(crawler_BlogDownloadedEvent);
            crawler.Crawl();
            //if (!mergeWithIndexPath.Equals(string.Empty))
            //    _luceneManager.MergeIndexes(mergeWithIndexPath);
            // _luceneManager.CloseIndexWriter();
        }

        public void DeepCrawlForums(string mergeWithIndexPath)
        {
            RuleSet rs = _site[RuleSet.Forum];
            if (null == rs) return;
            Crawler crawler = new Crawler(crawler_ForumDownloadedEvent, _workGroup, rs.CrawlDepth, _maxThreads, rs.Encoding, CrawlUrl.CrawlUrlType.Forum);
            crawler.Seed(rs.ContentUris);
            //crawler.HtmlDownloadedEvent += new EventHandler<HtmlDownloadEventArgs>(crawler_ForumDownloadedEvent);
            crawler.Crawl();
            // _luceneManager.CloseIndexWriter();
        }

        void crawler_StoryDownloadedEvent(HtmlDownloadEventArgs e)
        { 
            if (e.CrawlUrl.crawlUrlType == CrawlUrl.CrawlUrlType.Story)
                if (_indexer.IsContent(e.CrawlUrl.Uri, _site[RuleSet.Story].IdentifyingUris))
                _dal.SetUrlData(e.CrawlUrl.Uri, e.CrawlUrl.LastDownloadDate, _site.SiteNames.SiteName, e.CrawlUrl.crawlUrlType);
        }
        void crawler_BlogDownloadedEvent(HtmlDownloadEventArgs e)
        {
            if (e.CrawlUrl.crawlUrlType == CrawlUrl.CrawlUrlType.Blog)
                if (_indexer.IsContent(e.CrawlUrl.Uri, _site[RuleSet.Blog].IdentifyingUris))
                    _dal.SetUrlData(e.CrawlUrl.Uri, e.CrawlUrl.LastDownloadDate, _site.SiteNames.SiteName, e.CrawlUrl.crawlUrlType);
        }
        void crawler_ForumDownloadedEvent(HtmlDownloadEventArgs e)
        {
            if (e.CrawlUrl.crawlUrlType == CrawlUrl.CrawlUrlType.Forum)
                if (_indexer.IsContent(e.CrawlUrl.Uri, _site[RuleSet.Forum].IdentifyingUris))
                    _dal.SetUrlData(e.CrawlUrl.Uri, e.CrawlUrl.LastDownloadDate, _site.SiteNames.SiteName, e.CrawlUrl.crawlUrlType);
        }

    }
}
